/*
* LibraryController.java
* Created on  2014-7-30 下午2:30
* 版本       修改时间          作者      修改内容
* V1.0.1    2014-7-30       gaoxinyu    初始版本
*
*/
package com.genghis.ptas.task.library;

import com.genghis.core.autocomplete.service.AutoCompleteService;
import com.genghis.core.page.entity.Page;
import com.genghis.core.page.entity.PageParameter;
import com.genghis.ptas.constant.DictConstant;
import com.genghis.ptas.system.entity.DictValues;
import com.genghis.ptas.system.service.DictService;
import com.genghis.ptas.task.entity.LibraryTask;
import com.genghis.ptas.task.service.LibraryService;
import com.genghis.ptas.util.TaskFileUp;
import com.sun.net.httpserver.HttpContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

/**
 * 类的描述信息
 *
 * @author gaoxinyu
 * @version 1.0.1
 */
@Controller
@Transactional
@RequestMapping("/task/library/")
public class LibraryController {
    @Autowired
    private LibraryService libraryService;

    @Autowired
    private DictService dictService;

    @Autowired
    private AutoCompleteService autoCompleteService;

    @RequestMapping(value = "listAllLibraryTaskDo", method = {RequestMethod.GET, RequestMethod.POST})
    public String listAllLibraryTaskDo() {
        return "backstage/task/library/listAllTaskDo";
    }

    @RequestMapping(value = "listAllLibraryTask", method = RequestMethod.POST)
    @ResponseBody
    public Page listAllLibraryTask(PageParameter pageParameter, LibraryTask libraryTask) {
        return libraryService.findLibraryTaskPageList(pageParameter, libraryTask);
    }

    @RequestMapping(value = "addTaskDo", method = {RequestMethod.GET, RequestMethod.POST})
    public String addDictDefineDo(HttpServletRequest request) throws ServletException, IOException {
        request.setAttribute("content", TaskFileUp.file(request, "upFile"));
        List<DictValues> dictValuesList = dictService.findAllDictValues(DictConstant.TASKTYPE);
        request.setAttribute("type", dictValuesList);
        return "backstage/task/library/addTaskDo";
    }

    @RequestMapping(value = "analyzeLibraryTask", method = RequestMethod.POST)
    @ResponseBody
    public LibraryTask analyzeLibraryTask(LibraryTask libraryTask) {
        return libraryService.analyzeLibraryTask(libraryTask);
    }

    @RequestMapping(value = "addLibraryTask", method = RequestMethod.POST)
    @ResponseBody
    public void addLibraryTask(LibraryTask libraryTask) {
        libraryService.addLibraryTask(libraryTask);
    }

    @RequestMapping(value = "editLibraryTaskDo", method = {RequestMethod.GET, RequestMethod.POST})
    public String editLibraryTaskDo(HttpServletRequest request , Integer id) {
        LibraryTask libraryTask = libraryService.findLibraryTaskById(id);
        request.setAttribute("task", libraryTask);
        request.setAttribute("type", dictService.findAllDictValues(DictConstant.TASKTYPE));
        request.setAttribute("taskOption", libraryTask.getOptions());
        return "backstage/task/library/editTaskDo";
    }

    @RequestMapping(value = "deleteLibraryTask", method = RequestMethod.DELETE)
    @ResponseBody
    public void deleteLibraryTask(LibraryTask libraryTask) {
        libraryService.deleteLibraryTaskById(libraryTask.getId());
    }

    @RequestMapping(value = "editLibraryTask", method = RequestMethod.POST)
    @ResponseBody
    public void editLibraryTask(LibraryTask libraryTask) {
        libraryService.editLibraryTask(libraryTask);
    }
}
